package com.evil.application.enums.order;

import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * 订单查询类型
 *
 * @author 15587
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum StatisticsOrderTypeEnum {

    /**
     * 统计订单(关于我的)类型（0：我发起的，1：抄送事项，2：草稿箱，3：已办事项）
     */
    START(0, "我发起的", OrderQueryTypeEnum.START),
    CARBON(1, "抄送事项", OrderQueryTypeEnum.SEND),
    DRAFT_BOX(2, "草稿箱", OrderQueryTypeEnum.START),
    HANDLED(3, "已办事项", OrderQueryTypeEnum.HANDLED),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;
    @Getter
    private final OrderQueryTypeEnum queryTypeEnum;

    public static Optional<StatisticsOrderTypeEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static StatisticsOrderTypeEnum isExistById(Integer id) {
        return StatisticsOrderTypeEnum.findById(id).orElseThrow(StatisticsOrderTypeEnum::getBusinessException);
    }

    public static void forEach(Consumer<StatisticsOrderTypeEnum> action) {
        Arrays.asList(values()).forEach(action);
    }

    public static <R> Stream<R> stream(Function<StatisticsOrderTypeEnum, ? extends R> mapper) {
        return Arrays.stream(values()).map(mapper);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "统计订单类型参数错误");
    }
}
